﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataContexts;
using System.Xml;
using DataContexts.Supplier;

/// <summary>
/// Summary description for UserAccess
/// </summary>
public static class UserAccess
{
    private static IList<PageConfig> _pageConfigs;

    private const string SESSION_USER = "Session_User";
    private const string SESSION_SUPPLIER = "Session_Supplier";
    private const string SESSION_USER_TYPE = "Session_User_type";

    public static IList<PageConfig> PageConfig
    {
        get
        {
            if (_pageConfigs == null)
            {
                _pageConfigs = new List<PageConfig>();
                XmlDocument doc = new XmlDocument();
                doc.Load(HttpContext.Current.Server.MapPath("/PageConfig.xml"));
                XmlNode root = doc.SelectSingleNode("/Pages");
                foreach (XmlNode page in root.SelectNodes("Page"))
                    _pageConfigs.Add(new PageConfig(page));
            }
            return _pageConfigs;
        }
        set { _pageConfigs = value; }
    }
    public static bool IsCurrentUser(Enums.UserTypes userType)
    {
        if (CurrentUser != null)
            return (CurrentUser.UserType == Convert.ToInt32(userType));
        else
            return false;
    }
    public static DataContexts.User CurrentUser
    {
        get { return (HttpContext.Current.Session[SESSION_USER] != null) ? (DataContexts.User)HttpContext.Current.Session[SESSION_USER] : null; }
        set { HttpContext.Current.Session[SESSION_USER] = value; }
    }

    public static Supplier CurrentSupplier
    {
        get { return (HttpContext.Current.Session[SESSION_SUPPLIER] != null) ? (Supplier)HttpContext.Current.Session[SESSION_SUPPLIER] : null; }
        set { HttpContext.Current.Session[SESSION_SUPPLIER] = value; }
    }

    public static DataContexts.UserType CurrentUserType
    {
        get
        {
            if ((HttpContext.Current.Session[SESSION_USER_TYPE] == null))
            {
                UserDataContext context = new UserDataContext();
                HttpContext.Current.Session[SESSION_USER_TYPE] = context.UserTypes.SingleOrDefault(ut => ut.UserTypeId == CurrentUser.UserType);
            }
            return (DataContexts.UserType)HttpContext.Current.Session[SESSION_USER_TYPE];
        }
    }

    public static void ClearUserSession()
    {
        HttpContext.Current.Session[SESSION_USER] = null;
        HttpContext.Current.Session[SESSION_USER_TYPE] = null;
        HttpContext.Current.Session.Abandon();
    }

    public static bool AuthorizeCurrentUser(PageConfig page)
    {
        bool authUser = true;
        if (CurrentUser == null)
        {
            authUser = false;
            HttpContext.Current.Response.Redirect("/login.aspx", true);
        }
        else
        {
            PageConfig pc = PageConfig.SingleOrDefault(p => p.PageId == page.PageId);
            if (pc.AccessCode > CurrentUserType.AccessCode)
            {
                authUser = false;
                HttpContext.Current.Response.Redirect("/AccessDenied.aspx", true);
            }
        }

        return authUser;
    }

    public static bool AuthorizeCurrentUser(PageConfig.Pages page)
    {
        return AuthorizeCurrentUser(new PageConfig { PageId = Convert.ToInt32(page) });
    }
}
